﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using Microsoft.Reporting.WebForms;
using MockDataTransferObject;
namespace Presentation.Reporting
{
    public partial class TimeSheetReporting : System.Web.UI.Page
    {
        private UserBusiness _userBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;

        protected void Page_Init(object sender, EventArgs e)
        {
            _userBusiness = UserBusiness.RetrieveInstance();
            _warningLabel = (Label)Master.FindControl("WarningLabel");
            _errorLabel = (Label)Master.FindControl("ErrorLabel");
            _successLabel = (Label)Master.FindControl("SuccessLabel");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null && Session["user"] is User)
            {
                User user = (User)Session["User"];
                if (user.Role.RoleName != "Admin" & user.Role.RoleName != "Consultant")
                {
                    Response.Redirect("../NotAuthorized.aspx");
                }

                if (!Page.IsPostBack)
                {
                    DateTime nowDate = DateTime.Now;
                    DateTime startDate = new DateTime(nowDate.Year, nowDate.Month, 1);
                    FromDateTextBox.Text = String.Format("{0:MM/dd/yyyy}", startDate);
                    ToDateTextBox.Text = String.Format("{0:MM/dd/yyyy}", nowDate);
                    //Loading default
                    odsTimesheetDoc.SelectParameters["Statusname"].DefaultValue = StatusDropList.SelectedItem.ToString();
                    odsTimesheetDoc.SelectParameters["Username"].DefaultValue = "____";
                    odsTimesheetDoc.SelectParameters["FromDate"].DefaultValue = FromDateTextBox.Text;
                    odsTimesheetDoc.SelectParameters["ToDate"].DefaultValue = ToDateTextBox.Text;
                    odsTimesheetDoc.DataBind();
                    TimesheetDocReportViewer.LocalReport.Refresh();
                }
            }
            else
            {
                Response.Redirect("../LogOn.aspx");
            }


            _warningLabel.Text = "";
            _errorLabel.Text = "";
            _successLabel.Text = "";
        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            string username = UsernameTextBox.Text;
            User user = _userBusiness.GetByUserName(username);
            if (user == null)
            {
                _errorLabel.Text = "Username is not exist !";
                TimesheetDocReportViewer.Visible = false;
            }
            else if (user.Role.RoleName != "Contractor")
            {
                _warningLabel.Text = "Username is not contractor";
                TimesheetDocReportViewer.Visible = false;
            }
            else
            {
                TimesheetDocReportViewer.Visible = true;
                odsTimesheetDoc.SelectParameters["Statusname"].DefaultValue = StatusDropList.SelectedItem.ToString();
                odsTimesheetDoc.SelectParameters["Username"].DefaultValue = username;
                odsTimesheetDoc.SelectParameters["FromDate"].DefaultValue = FromDateTextBox.Text;
                odsTimesheetDoc.SelectParameters["ToDate"].DefaultValue = ToDateTextBox.Text;
                odsTimesheetDoc.DataBind();
                TimesheetDocReportViewer.LocalReport.Refresh();
            }
        }
    }
}